﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;



namespace SuperCoder
{
    public partial class frmCodeGenerate : Form
    {
       
        public List<string> listOfTables; //当前数据库的所有表；
        public List<string> listOfViews; //当前数据库的所有视图；


        public frmCodeGenerate()
        {
            InitializeComponent();
            frmConnectDB frmDB = new frmConnectDB();

            if (frmDB.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                this.Close();
            }
            this.listOfTables = frmDB.listOfTables;
            this.listOfViews = frmDB.listOfViews;
            frmDB.Close();

            InitConfigInfo();

        }

        #region "初始化配置信息；"
        /// <summary>
        /// 初始化配置信息；
        /// </summary>
        public void InitConfigInfo()
        {
            this.txtDBName.Text = Config.DB_NAME;
            this.txtServerIP.Text = Config.DB_SERVER;
            this.txtNameSpace.Text = Config.FRAMENAMESPACE;
            this.txtBLLPath.Text = Config.BLLCODE_PATH;
            this.txtDALPath.Text = Config.DALCODE_PATH;
            this.txtModelPath.Text = Config.MODELCODE_PATH;
            this.txtIDALPath.Text = Config.IDALCODE_PATH;
            this.txtDALFactoryPath.Text = Config.DALFACTORY_PATH;
            SwitchIDALAdnDALFactory(!rbFrameOfS3.Checked);
            if (listOfTables != null)
            {
                for (int i = 0; i < listOfTables.Count; ++i)
                {
                    lbTableOfSource.Items.Add(listOfTables[i]);
                }
            }
            if (listOfViews != null)
            {
                for (int i = 0; i < listOfViews.Count; ++i)
                {
                    lbTableOfSource.Items.Add(listOfViews[i]);
                }
            }
            btnDel.Enabled = false;
            btnDelList.Enabled = false;

        }
        #endregion


        #region "检查表单中的值；"
        /// <summary>
        /// 检查表单中的值；
        /// </summary>
        /// <returns></returns>
        private bool CheckFormConfig()
        {
            if (lbTableOfTarget.Items.Count <= 0)
            {
                MessageBox.Show("提示：请选择要生成的表或视图!");
                return false;
            }
            if (txtNameSpace.Text.Trim() == "")
            {
                MessageBox.Show("提示：命名空间不能为空！");
                return false;
            }

            if (txtBLLPath.Text.Trim() == "")
            {
                MessageBox.Show("提示：BLL层代码存储路径不能为空！");
                return false;
            }
            if (txtDALPath.Text.Trim() == "")
            {
                MessageBox.Show("提示：DAL层代码存储路径不能为空！");
                return false;
            }
            if (txtModelPath.Text.Trim() == "")
            {
                MessageBox.Show("提示：Model层代码存储路径不能为空！");
                return false;
            }
            if (rbFrameOfF3.Checked)
            {
                if (txtIDALPath.Text.Trim() == "")
                {
                    MessageBox.Show("提示：IDAL层代码存储路径不能为空！");
                    return false;
                }
                if (txtDALFactoryPath.Text.Trim() == "")
                {
                    MessageBox.Show("提示：DALFactory层代码存储路径不能为空！");
                    return false;
                }

            }

            return true;
        }
        #endregion


        #region "保存本次配置信息；"
        /// <summary>
        /// 保存本次配置信息；
        /// </summary>
        private void SaveFormConfig()
        {

        }
        #endregion

        /// <summary>
        /// 执行代码生成；
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            //检查各个配置；
            btnGenerate.Enabled = false;
            if (!CheckFormConfig())
            {
                btnGenerate.Enabled = true;
                return;
            }
            //保存本次配置信息；
            SaveFormConfig();

            //准备相关数据进行数据生成；



        }

        #region "存储代码路径选择"
        private void btnSelBLLPath_Click(object sender, EventArgs e)
        {
            SelectFolder(txtBLLPath);
        }

        private void btnSelModelPath_Click(object sender, EventArgs e)
        {
            SelectFolder(txtModelPath);
        }

        private void btnSelDALPath_Click(object sender, EventArgs e)
        {
            SelectFolder(txtIDALPath);
        }

        private void btnSelIDALPath_Click(object sender, EventArgs e)
        {
            SelectFolder(txtIDALPath);
        }

        private void btnSelDALFactoryPath_Click(object sender, EventArgs e)
        {
            SelectFolder(txtDALFactoryPath);
        }

        private void SelectFolder(TextBox txtObj)
        {
            if (fbDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtObj.Text = fbDialog.SelectedPath;
            }
        }
        #endregion

        #region "生成的数据表选择"
        private void btnAddList_Click(object sender, EventArgs e)
        {
            int c = lbTableOfSource.Items.Count;
          
            for (int i = 0; i < c; ++i)
            {
                lbTableOfTarget.Items.Add(lbTableOfSource.Items[i]);
            }
            lbTableOfSource.Items.Clear();

            IsHasItem();

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int c = this.lbTableOfSource.SelectedItems.Count;
            ListBox.SelectedObjectCollection objs = this.lbTableOfSource.SelectedItems;
            for (int i = 0; i < c; i++)
            {
                this.lbTableOfTarget.Items.Add(lbTableOfSource.SelectedItems[i]);

            }
            for (int i = 0; i < c; i++)
            {
                if (this.lbTableOfSource.SelectedItems.Count > 0)
                {
                    this.lbTableOfSource.Items.Remove(lbTableOfSource.SelectedItems[0]);
                }
            }
            IsHasItem();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            int c = this.lbTableOfTarget.SelectedItems.Count;
            ListBox.SelectedObjectCollection objs = this.lbTableOfTarget.SelectedItems;
            for (int i = 0; i < c; i++)
            {
                this.lbTableOfSource.Items.Add(lbTableOfTarget.SelectedItems[i]);

            }
            for (int i = 0; i < c; i++)
            {
                if (this.lbTableOfTarget.SelectedItems.Count > 0)
                {
                    this.lbTableOfTarget.Items.Remove(lbTableOfTarget.SelectedItems[0]);
                }
            }
            IsHasItem();
        }

        private void btnDelList_Click(object sender, EventArgs e)
        {
            int c = this.lbTableOfTarget.Items.Count;
            this.lbTableOfSource.Items.Clear();
            for (int i = 0; i < c; i++)
            {
                this.lbTableOfSource.Items.Add(this.lbTableOfTarget.Items[i]);
            }
            this.lbTableOfTarget.Items.Clear();
            IsHasItem();	
        }
        #endregion

        /// <summary>
        /// 判断listbox有没有项目
        /// </summary>
        private void IsHasItem()
        {
            if (this.lbTableOfSource.Items.Count > 0)
            {
                this.btnAddList.Enabled = true;
                this.btnAdd.Enabled = true;
            }
            else
            {
                this.btnAddList.Enabled = false;
                this.btnAdd.Enabled = false;
            }
            if (this.lbTableOfTarget.Items.Count > 0)
            {
                this.btnDel.Enabled = true;
                this.btnDelList.Enabled = true;
            }
            else
            {
                this.btnDel.Enabled = false;
                this.btnDelList.Enabled = false;
            }
        }

        #region "框架类型选择"
        private void rbFrameOfS3_CheckedChanged(object sender, EventArgs e)
        {
            SwitchIDALAdnDALFactory(false);
        }

        private void rbFrameOfF3_CheckedChanged(object sender, EventArgs e)
        {
            SwitchIDALAdnDALFactory(true);
        }
        #endregion

        #region " 控制IDAL,及DALFactory相关控件是否显示；"
        /// <summary>
        /// 控制IDAL,及DALFactory相关控件是否显示；
        /// </summary>
        /// <param name="isShow"></param>
        private void SwitchIDALAdnDALFactory(bool isShow)
        {
            txtIDALPath.Visible = isShow;
            lbIDALTip.Visible = isShow;
            btnSelIDALPath.Visible = isShow;

            txtDALFactoryPath.Visible = isShow;
            lbDALFactoryTip.Visible = isShow;
            btnSelDALFactoryPath.Visible = isShow;
        }
        #endregion



    }
}
